<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Liangguifeng\LotteryAnalyzer\Analyzer\DanmaAnalyzer;
use Liangguifeng\LotteryAnalyzer\Analyzer\DudanAnalyzer;
use Liangguifeng\LotteryAnalyzer\Analyzer\KillHundredOneSumTailAnalyzer;
use Liangguifeng\LotteryAnalyzer\Analyzer\KillHundredTenSumTailAnalyzer;
use Liangguifeng\LotteryAnalyzer\Analyzer\KillTenOneSumTailAnalyzer;

class HomeController extends Controller
{
    /**
     * 300+期测试数据.
     *
     * @var array
     */
    protected array $history = [
        '2024242' => [6, 2, 3],
        '2024243' => [9, 5, 5],
        '2024244' => [8, 6, 3],
        '2024245' => [7, 4, 1],
        '2024246' => [7, 1, 0],
        '2024247' => [7, 8, 3],
        '2024248' => [8, 6, 1],
        '2024249' => [4, 4, 9],
        '2024250' => [2, 5, 3],
        '2024251' => [1, 5, 6],
        '2024252' => [0, 8, 6],
        '2024253' => [2, 7, 4],
        '2024254' => [4, 0, 9],
        '2024255' => [0, 9, 6],
        '2024256' => [7, 4, 9],
        '2024257' => [1, 9, 9],
        '2024258' => [0, 7, 7],
        '2024259' => [2, 4, 9],
        '2024260' => [9, 2, 6],
        '2024261' => [7, 6, 6],
        '2024262' => [4, 6, 5],
        '2024263' => [2, 9, 2],
        '2024264' => [7, 1, 0],
        '2024265' => [2, 4, 5],
        '2024266' => [3, 9, 0],
        '2024267' => [2, 2, 7],
        '2024268' => [9, 3, 1],
        '2024269' => [3, 8, 8],
        '2024270' => [6, 9, 9],
        '2024271' => [7, 9, 5],
        '2024272' => [2, 7, 5],
        '2024273' => [8, 3, 3],
        '2024274' => [1, 7, 2],
        '2024275' => [8, 2, 7],
        '2024276' => [0, 8, 6],
        '2024277' => [6, 8, 4],
        '2024278' => [2, 2, 1],
        '2024279' => [0, 6, 0],
        '2024280' => [2, 4, 8],
        '2024281' => [9, 0, 0],
        '2024282' => [9, 8, 3],
        '2024283' => [4, 1, 9],
        '2024284' => [3, 4, 2],
        '2024285' => [7, 1, 8],
        '2024286' => [6, 2, 1],
        '2024287' => [1, 9, 2],
        '2024288' => [8, 3, 9],
        '2024289' => [9, 8, 9],
        '2024290' => [9, 1, 1],
        '2024291' => [4, 2, 6],
        '2024292' => [4, 6, 2],
        '2024293' => [5, 2, 1],
        '2024294' => [3, 6, 4],
        '2024295' => [5, 7, 1],
        '2024296' => [8, 1, 6],
        '2024297' => [3, 7, 2],
        '2024298' => [0, 5, 5],
        '2024299' => [9, 4, 9],
        '2024300' => [6, 6, 6],
        '2024301' => [4, 3, 5],
        '2024302' => [5, 1, 6],
        '2024303' => [3, 3, 6],
        '2024304' => [1, 6, 9],
        '2024305' => [9, 6, 6],
        '2024306' => [2, 9, 6],
        '2024307' => [5, 7, 9],
        '2024308' => [6, 5, 4],
        '2024309' => [5, 3, 9],
        '2024310' => [6, 0, 3],
        '2024311' => [0, 6, 7],
        '2024312' => [7, 5, 4],
        '2024313' => [0, 9, 5],
        '2024314' => [6, 7, 2],
        '2024315' => [8, 0, 2],
        '2024316' => [7, 8, 9],
        '2024317' => [3, 8, 6],
        '2024318' => [5, 2, 5],
        '2024319' => [7, 2, 6],
        '2024320' => [7, 7, 8],
        '2024321' => [5, 4, 2],
        '2024322' => [8, 6, 7],
        '2024323' => [5, 3, 6],
        '2024324' => [3, 8, 4],
        '2024325' => [1, 5, 8],
        '2024326' => [7, 2, 2],
        '2024327' => [2, 8, 0],
        '2024328' => [3, 0, 6],
        '2024329' => [9, 1, 0],
        '2024330' => [1, 6, 7],
        '2024331' => [9, 6, 4],
        '2024332' => [7, 6, 5],
        '2024333' => [9, 5, 1],
        '2024334' => [5, 4, 1],
        '2024335' => [5, 9, 3],
        '2024336' => [9, 6, 4],
        '2024337' => [8, 4, 3],
        '2024338' => [9, 7, 7],
        '2024339' => [5, 0, 9],
        '2024340' => [2, 7, 8],
        '2024341' => [3, 7, 1],
        '2024342' => [4, 8, 3],
        '2024343' => [2, 1, 4],
        '2024344' => [3, 0, 5],
        '2024345' => [3, 1, 7],
        '2024346' => [0, 5, 6],
        '2024347' => [3, 9, 2],
        '2024348' => [9, 2, 8],
        '2024349' => [1, 0, 8],
        '2024350' => [6, 7, 2],
        '2024351' => [6, 5, 7],
        '2024352' => [2, 9, 2],
        '2025001' => [7, 0, 3],
        '2025002' => [9, 1, 6],
        '2025003' => [2, 1, 4],
        '2025004' => [6, 3, 4],
        '2025005' => [0, 8, 5],
        '2025006' => [8, 9, 8],
        '2025007' => [1, 4, 1],
        '2025008' => [2, 6, 0],
        '2025009' => [4, 1, 6],
        '2025010' => [3, 3, 3],
        '2025011' => [5, 1, 8],
        '2025012' => [4, 8, 7],
        '2025013' => [6, 0, 6],
        '2025014' => [6, 7, 9],
        '2025015' => [9, 4, 2],
        '2025016' => [1, 0, 4],
        '2025017' => [0, 2, 7],
        '2025018' => [8, 3, 6],
        '2025019' => [7, 1, 0],
        '2025020' => [0, 5, 6],
        '2025021' => [4, 6, 8],
        '2025022' => [6, 3, 8],
        '2025023' => [5, 9, 1],
        '2025024' => [7, 6, 2],
        '2025025' => [4, 8, 6],
        '2025026' => [5, 8, 8],
        '2025027' => [3, 8, 3],
        '2025028' => [4, 0, 9],
        '2025029' => [0, 9, 6],
        '2025030' => [3, 0, 2],
        '2025031' => [3, 7, 9],
        '2025032' => [4, 9, 9],
        '2025033' => [6, 0, 6],
        '2025034' => [2, 6, 1],
        '2025035' => [7, 7, 8],
        '2025036' => [6, 1, 7],
        '2025037' => [1, 3, 1],
        '2025038' => [8, 4, 7],
        '2025039' => [9, 3, 2],
        '2025040' => [4, 2, 8],
        '2025041' => [4, 0, 1],
        '2025042' => [7, 7, 5],
        '2025043' => [4, 7, 5],
        '2025044' => [9, 1, 2],
        '2025045' => [5, 5, 4],
        '2025046' => [4, 9, 0],
        '2025047' => [2, 4, 8],
        '2025048' => [6, 4, 0],
        '2025049' => [0, 0, 4],
        '2025050' => [8, 0, 6],
        '2025051' => [9, 0, 5],
        '2025052' => [5, 8, 5],
        '2025053' => [8, 9, 5],
        '2025054' => [4, 9, 7],
        '2025055' => [3, 6, 8],
        '2025056' => [4, 9, 2],
        '2025057' => [3, 9, 7],
        '2025058' => [4, 0, 5],
        '2025059' => [8, 4, 7],
        '2025060' => [9, 3, 8],
        '2025061' => [4, 7, 6],
        '2025062' => [8, 1, 0],
        '2025063' => [4, 0, 5],
        '2025064' => [9, 2, 8],
        '2025065' => [5, 3, 8],
        '2025066' => [7, 2, 0],
        '2025067' => [3, 8, 5],
        '2025068' => [5, 5, 5],
        '2025069' => [9, 0, 6],
        '2025070' => [7, 4, 8],
        '2025071' => [4, 3, 4],
        '2025072' => [2, 7, 2],
        '2025073' => [7, 7, 8],
        '2025074' => [4, 1, 9],
        '2025075' => [7, 0, 9],
        '2025076' => [7, 8, 1],
        '2025077' => [6, 2, 5],
        '2025078' => [0, 0, 0],
        '2025079' => [6, 5, 7],
        '2025080' => [5, 0, 0],
        '2025081' => [4, 0, 3],
        '2025082' => [4, 1, 7],
        '2025083' => [9, 1, 5],
        '2025084' => [0, 3, 0],
        '2025085' => [3, 3, 8],
        '2025086' => [3, 8, 8],
        '2025087' => [8, 2, 6],
        '2025088' => [0, 2, 2],
        '2025089' => [6, 5, 8],
        '2025090' => [1, 5, 9],
        '2025091' => [1, 3, 5],
        '2025092' => [3, 1, 7],
        '2025093' => [5, 4, 5],
        '2025094' => [6, 4, 9],
        '2025095' => [0, 6, 8],
        '2025096' => [3, 8, 5],
        '2025097' => [5, 4, 2],
        '2025098' => [7, 1, 4],
        '2025099' => [7, 0, 7],
        '2025100' => [9, 5, 3],
        '2025101' => [0, 1, 6],
        '2025102' => [1, 6, 1],
        '2025103' => [3, 7, 0],
        '2025104' => [8, 7, 7],
        '2025105' => [0, 1, 5],
        '2025106' => [7, 5, 2],
        '2025107' => [0, 2, 7],
        '2025108' => [4, 0, 7],
        '2025109' => [8, 4, 1],
        '2025110' => [9, 1, 4],
        '2025111' => [2, 7, 0],
        '2025112' => [9, 5, 4],
        '2025113' => [6, 3, 5],
        '2025114' => [3, 7, 5],
        '2025115' => [2, 0, 6],
        '2025116' => [3, 2, 0],
        '2025117' => [8, 4, 9],
        '2025118' => [6, 2, 1],
        '2025119' => [3, 1, 5],
        '2025120' => [9, 1, 2],
        '2025121' => [1, 2, 0],
        '2025122' => [4, 8, 8],
        '2025123' => [7, 2, 5],
        '2025124' => [8, 0, 9],
        '2025125' => [3, 5, 1],
        '2025126' => [3, 2, 0],
        '2025127' => [4, 3, 1],
        '2025128' => [8, 1, 4],
        '2025129' => [9, 1, 5],
        '2025130' => [2, 0, 4],
        '2025131' => [2, 0, 3],
        '2025132' => [6, 8, 9],
        '2025133' => [6, 4, 5],
        '2025134' => [7, 4, 8],
        '2025135' => [4, 5, 8],
        '2025136' => [1, 1, 3],
        '2025137' => [8, 3, 8],
        '2025138' => [2, 9, 6],
        '2025139' => [5, 9, 8],
        '2025140' => [1, 4, 9],
        '2025141' => [2, 0, 2],
        '2025142' => [9, 1, 6],
        '2025143' => [2, 5, 6],
        '2025144' => [6, 6, 6],
        '2025145' => [2, 6, 7],
        '2025146' => [8, 9, 7],
        '2025147' => [3, 2, 1],
        '2025148' => [8, 3, 8],
        '2025149' => [7, 3, 4],
        '2025150' => [3, 2, 1],
        '2025151' => [7, 1, 8],
        '2025152' => [2, 1, 0],
        '2025153' => [0, 6, 9],
        '2025154' => [9, 2, 3],
        '2025155' => [4, 9, 9],
        '2025156' => [7, 7, 5],
        '2025157' => [9, 6, 9],
        '2025158' => [7, 0, 1],
        '2025159' => [0, 7, 3],
        '2025160' => [5, 0, 3],
        '2025161' => [7, 4, 8],
        '2025162' => [2, 7, 5],
        '2025163' => [6, 3, 6],
        '2025164' => [3, 3, 7],
        '2025165' => [6, 0, 9],
        '2025166' => [8, 0, 8],
        '2025167' => [7, 0, 2],
        '2025168' => [5, 1, 4],
        '2025169' => [9, 7, 8],
        '2025170' => [4, 7, 5],
        '2025171' => [1, 1, 3],
        '2025172' => [4, 0, 6],
        '2025173' => [0, 9, 2],
        '2025174' => [5, 3, 3],
        '2025175' => [6, 1, 6],
        '2025176' => [1, 2, 2],
        '2025177' => [4, 9, 7],
        '2025178' => [7, 2, 8],
        '2025179' => [2, 8, 0],
        '2025180' => [2, 6, 0],
        '2025181' => [8, 8, 6],
        '2025182' => [7, 0, 8],
        '2025183' => [7, 4, 4],
        '2025184' => [7, 9, 9],
        '2025185' => [1, 6, 4],
        '2025186' => [2, 2, 7],
        '2025187' => [7, 6, 1],
        '2025188' => [0, 1, 8],
        '2025189' => [9, 0, 7],
        '2025190' => [4, 0, 2],
        '2025191' => [2, 0, 2],
        '2025192' => [6, 9, 1],
        '2025193' => [8, 7, 6],
        '2025194' => [7, 2, 6],
        '2025195' => [9, 8, 0],
        '2025196' => [7, 3, 4],
        '2025197' => [0, 5, 7],
        '2025198' => [8, 9, 8],
        '2025199' => [9, 4, 3],
        '2025200' => [0, 1, 5],
        '2025201' => [8, 4, 6],
        '2025202' => [9, 5, 3],
        '2025203' => [0, 1, 3],
        '2025204' => [0, 0, 7],
        '2025205' => [9, 2, 0],
        '2025206' => [4, 4, 4],
        '2025207' => [2, 5, 5],
        '2025208' => [4, 3, 2],
        '2025209' => [3, 8, 7],
        '2025210' => [5, 2, 0],
        '2025211' => [8, 9, 7],
        '2025212' => [4, 5, 6],
        '2025213' => [3, 8, 1],
        '2025214' => [9, 2, 0],
        '2025215' => [8, 5, 3],
        '2025216' => [6, 2, 5],
        '2025217' => [8, 9, 4],
        '2025218' => [5, 2, 6],
        '2025219' => [9, 0, 0],
        '2025220' => [5, 3, 6],
        '2025221' => [2, 9, 6],
        '2025222' => [0, 1, 0],
        '2025223' => [7, 3, 6],
        '2025224' => [9, 9, 0],
        '2025225' => [8, 0, 6],
        '2025226' => [1, 8, 0],
        '2025227' => [6, 1, 1],
        '2025228' => [4, 6, 5],
        '2025229' => [1, 0, 8],
        '2025230' => [4, 3, 9],
        '2025231' => [7, 3, 2],
        '2025232' => [0, 4, 4],
        '2025233' => [2, 5, 9],
        '2025234' => [6, 2, 0],
        '2025235' => [9, 6, 9],
        '2025236' => [3, 5, 5],
        '2025237' => [5, 9, 0],
        '2025238' => [8, 6, 0],
        '2025239' => [1, 2, 5],
        '2025240' => [4, 1, 9],
        '2025241' => [1, 6, 0],
        '2025242' => [9, 9, 0],
        '2025243' => [1, 7, 6],
        '2025244' => [6, 7, 3],
        '2025245' => [8, 4, 8],
        '2025246' => [9, 9, 7],
        '2025247' => [2, 6, 2],
    ];

    public function form(Request $request)
    {
        return view('lottery.form');
    }

    public function index(Request $request)
    {
        $currentPage = $request->page ?? 1;
        $perPage = $request->perPage ?? 20;
        $type = $request->type ?? 1;
        $analyzePeriods = $request->analyzePeriods ?? 3;
        $intervalPeriods = $request->intervalPeriods ?? 0;
        $minConsecutive = $request->minConsecutive ?? 3;
        $combinationSize = $request->combinationSize ?? 3;

        $data = $this->getAnalyzer($type)->withMaxConsecutive(true)->analyze($analyzePeriods, $minConsecutive, $combinationSize, $intervalPeriods);

        foreach ($data['hit_list'] as &$hit) {
            $items = array_values($hit['items']); // 确保索引连续
            $total = count($items);
            $currentItems = array_slice($items, ($currentPage - 1) * $perPage, $perPage);

            $hit['items_paginated'] = new LengthAwarePaginator(
                $currentItems,
                $total,
                $perPage,
                $currentPage,
                ['path' => url()->current()]
            );
        }

        return view('lottery.index', compact('data'));
    }

    public function convert(Request $request)
    {
    }

    private function getAnalyzer($type)
    {
        switch ($type) {
            case 1:
                $analyzer = new DanmaAnalyzer($this->history);
                break;
            case 2:
                $analyzer = new DudanAnalyzer($this->history);
                break;
            case 3:
                $analyzer = new KillHundredTenSumTailAnalyzer($this->history);
                break;
            case 4:
                $analyzer = new KillHundredOneSumTailAnalyzer($this->history);
                break;
            case 5:
                $analyzer = new KillTenOneSumTailAnalyzer($this->history);
                break;
            default:
                $analyzer = new DanmaAnalyzer($this->history);
        }

        return $analyzer;
    }
}
